/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.zbxsoft.utr.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidSessionManager;
import com.apache.cache.util.Validator;

/**
 * description:  继续启用活动（只是修改活动状态）
 * @author xhe 创建时间：2016-03-21  
 * 
 */
public class UnityContinueSessionPluginImpl implements PluginConnector {
	private Logger log = Logger.getLogger(getClass());
	protected BidSessionManager bidSessionManager;
	protected BidItemManager bidItemManager;

	public Object execute(ParamsVo param) throws Exception {
		String sessionId = Validator.getDefaultStr(param.getInfoId(), String.valueOf(param.getParams("infoId")));
		log.info("开始继续启用活动...");
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");

		if (Validator.isNull(sessionId)) {
			entity.setResult("false");
			entity.setMessage("缺少方法参数，继续启用活动失败");
			return entity;
		}
		//判断规则
		ParamsVo<BidSession> svo = new ParamsVo<BidSession>();
		svo.setInfoId(sessionId);
		BidSession session = (BidSession) bidSessionManager.getInfoById(svo);
		if (Validator.isEmpty(session)) {
			entity.setResult("false");
			entity.setMessage("没有获取到活动信息，继续启用操作失败");
			return entity;
		}
		String status = session.getStatus();
		if (Constant.getInstance().getSessionConfig("statusPubslish").equals(status)) {
			entity.setResult("false");
			entity.setMessage("此活动已经发布，不需要重复发布操作");
			return entity;
		}
		//更新缓存
		session.setStatus(Constant.getInstance().getSessionConfig("statusPubslish"));
		svo.setObj(session);
		boolean mark = bidSessionManager.editInfo(svo);

		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("session");
		String cacheKey = cache.session_prefix + sessionId;
		List list = null;
		if (cache.checkCacheObject(cacheKey)) {
			mark = cache.updateCacheObject(session);//存在就更新
			List<String> itemIds = (List) cache.getCacheCloneByKey("bidSessionItemIds_" + sessionId);
			if (!Validator.isEmpty(itemIds)) {
				list = cache.getObjects(itemIds, cache.item_prefix);
			}
		} else {
			mark = cache.createCacheObject(session);//不存在就创建
		}
		//以下处理标的信息
		if (Validator.isEmpty(list)) {
			ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
			BidItem item = new BidItem();
			item.setItemStatus(Constant.getInstance().getItemConfig("statusSuspend"));
			item.setSessionId(sessionId);
			vo.setObj(item);
			vo.setMethodKey("ByCacheList");
			list = bidItemManager.getList(vo);
		}
		if (!Validator.isEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				BidItemCacheVo cacheVo = (BidItemCacheVo) list.get(i);
				UnityItemPluginHelper.getInstance().resetItem(cacheVo, true);
			}
		}
		if (mark) {
			entity.setMessage("操作成功");
			entity.setEntity("true");
		} else {
			entity.setMessage("操作失败、缓存更新失败");
			entity.setEntity("false");
		}
		log.info("继续启用活动，处理完成！");
		return entity;
	}

	public void setBidSessionManager(BidSessionManager bidSessionManager) {
		this.bidSessionManager = bidSessionManager;
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

}
